use rand::distr::uniform::{SampleRange, SampleUniform};
use rand::distr::Alphanumeric;
use rand::{thread_rng, Rng};

const CHAR_NUM_OHTER_VEC: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789)(*&^%$#@!~";
const CHAR_NUM_VEC: &[u8] = b"abcdefghijklmnopqrstuvwxyz0123456789";
const CHAR16_NUM_VEC: &[u8] = b"abcdef0123456789";

const CHARSET: &[u8] = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

// 生成单个16位随机字符串
pub fn generate_random_string(len:u8) -> String {
    let mut rng = thread_rng();
    (0..len)
        .map(|_| {
            let idx = rng.gen_range(0..CHARSET.len());
            CHARSET[idx] as char
        })
        .collect()
}

/**
 * 随机生成UUID
 */
pub fn rand_uuid() -> String {
    (0..36).map(|temp|{
        match temp {
            8|13|18|23 => {
                '-'
            }
            _ => {
                let sel_num = rand_num(0..CHAR16_NUM_VEC.len());
                CHAR16_NUM_VEC[sel_num] as char
            }
        }

    }).collect()
}


pub fn rand_str_zd(n: usize) -> String {
    rand_str_char(n, CHAR_NUM_VEC)
}

/**
 * 根据字符集，选指定的
 */
pub fn rand_str_char(n: usize, charest: &[u8]) -> String {

    (0..n).map(|_|{
        let sel_num = rand_num(0..charest.len());
        charest[sel_num] as char
    }).collect()
}

/**
 * 随机生成指定常数的字符串 A-Z a-z 0-9之间
 */
pub fn rand_str(n: usize) -> String {
    let str = thread_rng()
        .sample_iter(&Alphanumeric)
        .take(n)
        .map(char::from)
        .collect();
    return str;
}

/**
 * 打印一定范围的随机数
 */
pub fn rand_num<T, R>(range: R) -> T
where
    T: SampleUniform,
    R: SampleRange<T>,
{
    let mut trn = thread_rng();
    return trn.gen_range(range);
}














